package com.shop.cloud.service.hongfu.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.shop.cloud.domain.hongfu.Bank;
import com.shop.cloud.domain.hongfu.BankAccount;
import com.shop.cloud.domain.hongfu.User;
import com.shop.cloud.dao.hongfu.BankAccountDao;
import com.shop.cloud.service.hongfu.IBankAccountService;
import com.shop.cloud.util.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;

/**
 * 
 */
@Service("bankAccountService")
public class BankAccountService extends BaseService<BankAccount> implements IBankAccountService{
    
    @Autowired
	private BankAccountDao bankAccountDao;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private BankService bankService;
    
    @Autowired
    private BankAccountService bankAccountService;

	@Override
	public MybatisDao<BankAccount> getEntityDao() {
		return bankAccountDao;
	}

	@Override
	public int handleAccount(BankAccount bankAccount) {
		// TODO Auto-generated method stub
		BankAccount bankAccount3 = new BankAccount();
		bankAccount3.setUserId(bankAccount.getUserId());
		List<BankAccount> list = bankAccountDao.findByEntity(bankAccount3);
		int flag = 0;
		if (list !=null && list.size()>0) {
			
			for (BankAccount bankAccount2 : list) {
				//银行卡是否绑定过
				if (bankAccount2.getCardNo().equals( bankAccount.getCardNo())) {
					return 0 ;
				}
			}
			bankAccount.setIsdefault(1);
			bankAccount.setIsbank(0);
			bankAccount.setCreateTime(new Timestamp((new Date()).getTime()));
			flag = bankAccountDao.insert(bankAccount);
		}else {
			bankAccount.setIsdefault(0);
			bankAccount.setIsbank(0);
			bankAccount.setCreateTime(new Timestamp((new Date()).getTime()));
			flag = bankAccountDao.insert(bankAccount);
			//修改user表中绑定状态
			User user = new User();
			user.setIsBank(0);
			user.setId(bankAccount.getUserId());
			userService.update(user);
		}
		
		
		return flag;
	}

	@Override
	public Page<BankAccount> HandlePage(BankAccount bankAccount) {
		// TODO Auto-generated method stub
		
		bankAccount.setPageSize((bankAccount.getPageSize_()==null?20:bankAccount.getPageSize_()));
		long count = bankAccountDao.countByEntity(bankAccount);
		Page<BankAccount> page = new Page<BankAccount>(bankAccount.getPageIndex_(), bankAccount.getPageSize_());
		List<BankAccount> list = bankAccountDao.findByEntity(bankAccount);
		for (BankAccount bankAccount2 : list) {
			Bank bank = bankService.get(bankAccount2.getBankId());
			bankAccount2.setBankName(bank.getName());
		}
		page.setData(list);
		page.setTotalCount(count);
		return page;
	}

	@Override
	public int HandleUpdate(BankAccount bankAccount2) {
		// TODO Auto-generated method stub
		return bankAccountDao.HandleUpdate(bankAccount2);
	}

	@Override
	public int updateBankAccount(BankAccount bankAccount) {
		// TODO Auto-generated method stub
		
		BankAccount bankAccount2 = bankAccountService.get(bankAccount.getId());
		//是否解绑默认的账户
		if (bankAccount2.getIsdefault()==0) {
			BankAccount bankAccount3 = new BankAccount();
			bankAccount3.setOrderBy("BANK_ACCOUNT.create_time desc");
			bankAccount3.setUserId(bankAccount.getUserId());
			bankAccount3.setIsbank(0);
			//将最新添加的账户设为默认
			List<BankAccount> BList = bankAccountService.find(bankAccount3);
			if (BList.get(0).getId().equals(bankAccount.getId())) {
				if (BList.get(1).getId() == null) {
					return 0;
				}
				bankAccount3.setId(BList.get(1).getId());
			}else {
				bankAccount3.setId(BList.get(0).getId());
			}
			bankAccount3.setIsdefault(0);
			bankAccountService.update(bankAccount3);
		}
		bankAccount.setIsbank(1);	
		return bankAccountService.update(bankAccount);
	}
    
}



















